***************************************************************
* Afrobarometer R9 x ACLED – Cleaned, Replicable Pipeline
* (copy–paste into a .do file)
***************************************************************
//--------------------------------------------------------------
// 02_analysis.do — Replication cleaning script
// Project: "The Impact of Terrorism on Democratic Support in Africa"
//--------------------------------------------------------------
version 18.0
clear all
set more off
set rmsg on
cap set scheme plotplain

* --------------------------------------------------------------
* Project root (robust to where the do-file is run from)
* --------------------------------------------------------------
local thisdo = c(filename)

* Fallback if c(filename) is empty (rare)
if "`thisdo'" == "" {
    global root "`c(pwd)'"
}
else {
    * Directory containing this do-file
    local dodir : dirname "`thisdo'"
    * Project root is parent of /dofile
    local root  : dirname "`dodir'"
    global root "`root'"
}

* If someone runs from inside /dofile (extra safety)
if substr("${root}", -6, 6) == "dofile" {
    global root = substr("${root}", 1, length("${root}")-6)
}

*--------------------------------------------------------------
* 0. PATHS (relative; no machine-specific paths)
*--------------------------------------------------------------

global data   "${root}/Original data"
global do     "${root}/dofile"
global out    "${root}/output"
global table  "${out}/tables"
global graph  "${out}/graphs"
global data_new  "${out}/Generated data"

cap mkdir "${out}"
cap mkdir "${table}"
cap mkdir "${graph}"
cap mkdir "${data_new}"

cap log close _all
log using "${out}/02_analysis.log", text replace name(rep)

***************************************************************
* 1. LOAD MICRO DATA & DEFINE COVARIATES / OUTCOMES
***************************************************************

use "${data}/R9_final.dta", clear

//use "${main}data_final.dta", clear

* Ensure surveyyear exists (for FE)
capture confirm variable surveyyear
if _rc {
    capture confirm variable year
    if !_rc gen surveyyear = year
    label var surveyyear "Survey year (from interview date)"
}

* Covariates for weighting & regressions
global cvars age gender race_group religion ethnic urban_rural educ_group emp_group ///
             safety fearing_crime voted discuss_politics ///
             police_station soldiers_army piped_water

* Same covariates with factor notation for balance regressions (Appendix)
global cvars_demographics age gender ib1.race_group ib1.religion ethnic ///
                           ib1.urban_rural ib4.educ_group ///
                           safety fearing_crime voted ///
                           police_station soldiers_army piped_water

* Outcomes (original scale)
global outcomes_groups demo_support auth_support demo_rated

***************************************************************
* 2. RESTRICT SAMPLE: COUNTRIES WITH BOTH TREATED & CONTROL
***************************************************************

preserve
    gen flag_0 = (time_zero == 0)
    gen flag_1 = (time_zero == 1)

    collapse (max) flag_0 flag_1, by(cntrynum)

    gen keep_country = (flag_0 == 1 & flag_1 == 1)
    keep if keep_country

    keep cntrynum
    tempfile good_countries
    save `good_countries'
restore

* Keep only countries that have both time_zero values
merge m:1 cntrynum using `good_countries', keep(match) nogen

* Save restricted dataset (used later for country plots)
save "${data_new}/data_goodcountries.dta", replace

***************************************************************
* 3. STANDARDISE OUTCOMES (0–1) IN RESTRICTED SAMPLE
***************************************************************

* z-prefix is min–max scaling to [0,1]
foreach v of varlist $outcomes_groups {
    qui summ `v'
    gen z`v' = (`v' - r(min)) / (r(max) - r(min))
}

* Main z-outcomes used throughout
global zoutcomes_group1 zdemo_support zauth_support zdemo_rated

summarize $cvars
summarize $zoutcomes_group1

***************************************************************
* 4. MAIN REGRESSION (TABLE 1) WITH ENTROPY BALANCING
***************************************************************

* Treatment indicator
global treatments time_zero 

* Entropy balancing weights
//drop balance_zero
ebalance time_zero $cvars, generate(balance_zero) targets(3)
svyset [pweight = balance_zero]

capture erase "${table}/t1_e.xls"
capture erase "${table}/t1_e.rtf"
estimates clear

* Make sure esttab / eststo / estadd are available
cap which esttab
if _rc ssc install estout, replace

local models

foreach k of varlist $zoutcomes_group1 {
    
    *--------------------------
    * 1) Run model
    *--------------------------
    quietly svy: reg `k' i.time_zero $cvars i.cntrynum i.surveyyear
    
    *--------------------------
    * 2) Control-group summary
    *--------------------------
    quietly summarize `k' if time_zero == 0
    local m   = r(mean)
    local min = r(min)
    local max = r(max)

    *--------------------------
    * 3) Short titles for table
    *--------------------------
    local short_title ""
    if "`k'" == "zdemo_support"    local short_title "Support for democracy"
    if "`k'" == "zauth_support"    local short_title "Reject authoritarianism"
    if "`k'" == "zdemo_rated"      local short_title "Democracy rating"

    *--------------------------
    * 4) Add custom stats to e()
    *--------------------------
    qui estadd scalar cmean = `m'
    qui estadd scalar cmin  = `min'
    qui estadd scalar cmax  = `max'
    qui estadd local  pretreat "Yes"

    * Store model with nice title
    eststo, title("`short_title'")
    local models `"`models' `e(name)'"'
}

*------------------------------------------
* PRINT TABLE TO RESULTS WINDOW
*------------------------------------------
esttab `models', ///
    keep(1.time_zero) ///
    b(3) se level(95) brackets ///
    star(* 0.05 ** 0.01 *** 0.001) ///
    label nonotes noobs nobaselevels ///
    coeflabels(1.time_zero "Terrorism exposure") ///
    mtitle ///
    stats(N r2 pretreat cmean, ///
          fmt(0 3 0 3) ///
          labels("Observations" "R^2" "Pre-treatment" "Control Mean" "Min" "Max")) ///
    compress
*-------------------------------*
*Export CSV
*-------------------------------*
esttab `models' using "${table}/t1_entropybal.csv", replace csv ///
    keep(1.time_zero) ///
    coeflabels(1.time_zero "Terrorism exposure") ///
    b(3) se level(95) brackets ///
    star(* 0.05 ** 0.01 *** 0.001) ///
    mtitle ///
    label nonotes noobs nobaselevels ///
    stats(N r2 pretreat cmean, ///
          fmt(0 3 0 3) ///
          labels("Observations" "R^2" "Pre-treatment" "Control mean" "Min" "Max")) ///
    compress
***************************************************************
* 5. FIGURE 1 – LIBERAL DEMOCRACY vs HDI (COUNTRY MEANS)
***************************************************************

preserve
  
    capture confirm variable hdi
    if _rc {
        capture confirm variable HumanDevelopmentIndexHDI
        if !_rc rename HumanDevelopmentIndexHDI hdi
    }

    collapse (mean) v2x_libdem hdi, by(cntrynum)

    twoway scatter v2x_libdem hdi, ///
        mlabel(cntrynum) mlabangle(45) mlabposition(12) mlabsize(vsmall) ///
        msymbol(O) msize(vsmall) ///
        xlabel(0.3(.1).8) ylabel(0(.1).6) ///
        xtitle("Human Development Index (HDI)") ///
        ytitle("Liberal Democracy Index (LDI)") ///
        legend(off) aspect(0.8)

    graph save   "${graph}/vdemhdi.gph", replace
    graph export "${graph}/vdemhdi.eps", replace
    graph export "${graph}/vdemhdi.pdf", as(pdf) replace
    graph export "${graph}/vdemhdi.png", width(2000) replace
restore

***************************************************************
* 6. FIGURE 2 – MARGINAL EFFECT BY FATALITIES
***************************************************************

* Categorise fatalities
capture drop fatalities_cat
gen fatalities_cat = .
replace fatalities_cat = 0 if fatalities == 0
replace fatalities_cat = 1 if inrange(fatalities, 1, 9)
replace fatalities_cat = 2 if fatalities >= 10

label define fatalities_cat_lbl ///
    0 "No (0)" ///
    1 "Low (1–9)" ///
    2 "High (10+)"
label values fatalities_cat fatalities_cat_lbl
label variable fatalities_cat "Fatalities category"
tab fatalities_cat

foreach k of varlist $zoutcomes_group1 {
    local title_label ""
    if "`k'" == "zdemo_support" local title_label "Democratic support"
    if "`k'" == "zauth_support" local title_label "Rejection of authoritarian alternatives"
    if "`k'" == "zdemo_rated"  local title_label "Democracy rating"

    svy: reg `k' i.time_zero##i.fatalities_cat $cvars i.surveyyear

    margins, dydx(time_zero) at(fatalities_cat = (0(1)2))
    marginsplot, ylabel(-.4(.1).4) xlabel(, labsize(medium)) yline(0) ///
        xtitle("Fatalities", size(medium)) ///
        ytitle("Effects of treatments on outcome", size(small)) ///
        title("DV: `title_label'", size(medium)) ///
        name(`k', replace) recastci(rspike) recast(scatter) ///
        scale(.7) legend(off)
}

graph combine $zoutcomes_group1
graph save   "${graph}/fatalities.gph", replace
graph export "${graph}/fatalities.eps", replace
graph export "${graph}/fatalities.pdf", as(pdf) replace
graph export "${graph}/fatalities.png", width(2000) replace

***************************************************************
* 7. FIGURE 3 – MARGINAL EFFECT BY LDI (v2x_libdem)
***************************************************************

foreach k of varlist $zoutcomes_group1 {
    local title_label ""
    if "`k'" == "zdemo_support" local title_label "Democratic support"
    if "`k'" == "zauth_support" local title_label "Rejection of authoritarian alternatives"
    if "`k'" == "zdemo_rated"  local title_label "Democracy rating"

    svy: reg `k' i.time_zero##c.v2x_libdem $cvars i.surveyyear

    margins, dydx(time_zero) at(v2x_libdem = (0(0.1).7))
    marginsplot, ylabel(-.4(.1).4) xlabel(, labsize(medium)) yline(0) ///
        xtitle("Liberal democracy index score", size(medium)) ///
        ytitle("Effects of treatments on outcome", size(small)) ///
        title("DV: `title_label'", size(medium)) ///
        name(`k', replace) recastci(rspike) recast(scatter) ///
        scale(.8) legend(off)
}

graph combine $zoutcomes_group1
graph save   "${graph}/libdem.gph", replace
graph export "${graph}/libdem.eps", replace
graph export "${graph}/libdem.pdf", as(pdf) replace

***************************************************************
* 8. FIGURE 4 – MARGINAL EFFECT BY HDI
***************************************************************

foreach k of varlist $zoutcomes_group1 {
    local title_label ""
    if "`k'" == "zdemo_support" local title_label "Democratic support"
    if "`k'" == "zauth_support" local title_label "Rejection of authoritarian alternatives"
    if "`k'" == "zdemo_rated"  local title_label "Democracy rating"

    svy: reg `k' i.time_zero##c.hdi $cvars i.surveyyear

    margins, dydx(time_zero) at(hdi = (0.3(0.1).7))
    marginsplot, ylabel(-.3(.1).3) xlabel(, labsize(medium)) yline(0) ///
        xtitle("Human development index score", size(medium)) ///
        ytitle("Effects of treatments on outcome", size(medsmall)) ///
        title("DV: `title_label'", size(medium)) ///
        name(`k', replace) recastci(rspike) recast(scatter) ///
        scale(.8) legend(off)
}

graph combine $zoutcomes_group1
graph save   "${graph}/hdi.gph", replace
graph export "${graph}/hdi.eps", replace
graph export "${graph}/hdi.pdf", as(pdf) replace

***************************************************************
* 9. FIGURE 5 – HETEROGENEITY BY GENDER
***************************************************************

svyset, clear
global cvar_gender age race_group religion ethnic urban_rural educ_group emp_group ///
                   safety fearing_crime voted discuss_politics ///
                   police_station soldiers_army piped_water 

*drop balance_gender
ebalance time_zero $cvar_gender, generate(balance_gender) targets(3)
svyset [pweight = balance_gender]

foreach k of varlist $zoutcomes_group1 {
    local title_label ""
    if "`k'" == "zdemo_support" local title_label "Democratic support"
    if "`k'" == "zauth_support" local title_label "Rejection of authoritarian alternatives"
    if "`k'" == "zdemo_rated"  local title_label "Democracy rating"

    svy: reg `k' i.time_zero##i.gender $cvar_gender i.surveyyear i.cntrynum
    margins, dydx(time_zero) at(gender = (0 1))

    marginsplot, ylabel(-.2(.05).2) xlabel(, labsize(medium)) yline(0) ///
        xtitle("Gender", size(medium)) ///
        ytitle("Effects of treatments on outcome", size(small)) ///
        title("DV: `title_label'", size(medium)) ///
        name(`k', replace) recastci(rspike) recast(scatter) ///
        scale(.8) legend(off)
}

graph combine $zoutcomes_group1
graph save   "${graph}/gender.gph", replace
graph export "${graph}/gender.eps", replace
graph export "${graph}/gender.pdf", as(pdf) replace

***************************************************************
* 10. FIGURE 6 – HETEROGENEITY BY AGE GROUP
***************************************************************

* Recode age into groups
capture drop age_gr
recode age ///
    (18/34 = 1) ///
    (35/44 = 2) ///
    (45/103 = 3), generate(age_gr)

label define age_groups_lbl 1 "18–34" 2 "35–44" 3 "45+"
label values age_gr age_groups_lbl
label variable age_gr "Age groups"
tab age_gr

svyset, clear
global cvar_age gender race_group religion ethnic urban_rural educ_group emp_group ///
                safety fearing_crime voted discuss_politics ///
                police_station soldiers_army piped_water 

*drop balance_age
ebalance time_zero $cvar_age, generate(balance_age) targets(3)
svyset [pweight = balance_age]

foreach k of varlist $zoutcomes_group1 {
    local title_label ""
    if "`k'" == "zdemo_support" local title_label "Democratic support"
    if "`k'" == "zauth_support" local title_label "Rejection of authoritarian alternatives"
    if "`k'" == "zdemo_rated"  local title_label "Democracy rating"

    svy: reg `k' i.time_zero##i.age_gr $cvar_age i.surveyyear i.cntrynum
    margins, dydx(time_zero) at(age_gr = (1(1)3))

    marginsplot, ylabel(-.15(.05).15) xlabel(, labsize(medium)) yline(0) ///
        xtitle("Age", size(medium)) ///
        ytitle("Effects of treatment on outcome", size(small)) ///
        title("DV: `title_label'", size(medium)) ///
        name(`k', replace) recastci(rspike) recast(scatter) ///
        scale(.8) legend(off)
}

graph combine $zoutcomes_group1
graph save   "${graph}/age.gph", replace
graph export "${graph}/age.eps", replace
graph export "${graph}/age.pdf", as(pdf) replace

***************************************************************
* 11. FIGURE 7 – COUNTRY-SPECIFIC EFFECTS (COEFPLOTS)
***************************************************************

use "${data_new}/data_goodcountries.dta", clear

* z-outcomes should already exist in this file; if not, recreate:
capture confirm variable zdemo_support
if _rc {
    global outcomes_groups demo_support auth_support demo_rated
    foreach v of varlist $outcomes_groups {
        qui summ `v'
        gen z`v' = (`v' - r(min)) / (r(max) - r(min))
    }
}
global zoutcomes_group1 zdemo_support zauth_support zdemo_rated
su $zoutcomes_group1
encode country, gen(cntry)

local countr  `" "Angola" "Benin" "Burkina Faso" "Cameroon" "Ghana" "Guinea" "Kenya" "Morocco" "Niger" "Senegal" "Sierra Leone" "Tanzania" "Zimbabwe" "'
local outcomes "zdemo_support zauth_support zdemo_rated"

local n_countr : word count `countr'
local graphlist
estimates clear

* Re-define covariates for this section 
global cvars age gender race_group religion ethnic urban_rural educ_group emp_group ///
             safety fearing_crime voted discuss_politics ///
             police_station soldiers_army piped_water
			 
ebalance time_zero $cvars, generate(balance_zero) targets(3)
svyset [pweight = balance_zero]

* NOTE: you may want to re-set svy weights here as above if needed.

forvalues i = 1/`n_countr' {
    local cntrylbl : word `i' of `countr'
    local estlist

    foreach var of local outcomes {
        quietly capture svy: reg `var' i.time_zero $cvars i.surveyyear if cntry == `i'
        if !_rc {
            estimates store est_`var'_`i'
            local estlist `estlist' est_`var'_`i'
        }
    }

    if "`estlist'" != "" {
        local gname g_`i'

        capture noisily coefplot `estlist', ///
            keep(1.time_zero) ///
            drop(_cons) ///
            xline(0, lcolor(gs10)) ///
            coeflabels(1.time_zero = " ") ///
            plotlabels("Support for democracy" ///
                       "Rejection of authoritarianism" ///
                       "Democracy rating") ///
            legend(rows(1)) ///
            title("`cntrylbl'", size(medsmall)) ///
            legend(off) ///
            ylabel(, labsize(small)) ///
            xlabel(-0.4(0.2)0.25, labsize(small)) ///
            name(`gname', replace) ///
            ysize(2) xsize(5)

        capture confirm graph `gname'
        if !_rc local graphlist `graphlist' `gname'
    }
}

* Combine graphs (manual list if preferred)
	grc1leg g_1 g_2 g_3 g_4 g_5 g_6 g_7 g_8 g_9 g_10 g_11 g_12 g_13, ///
    cols(3) imargin(2 2 2 2) xsize(8) ysize(7)

graph save   "${graph}/DV1_countries.gph", replace
graph export "${graph}/DV1_countries.pdf", as(pdf) replace

//******************************************************
// APPENDIX
//******************************************************

***************************************************************
* Appendix C – Balance and weighting procedures (Main sample)
***************************************************************

* Use restricted sample again
use "${data_new}/data_goodcountries.dta", clear

* Same covariates with factor notation for balance regressions (Appendix)
global cvars_demographics age gender ib1.race_group ib1.religion ethnic ///
                           ib1.urban_rural ib4.educ_group ///
                           safety fearing_crime voted ///
                           police_station soldiers_army piped_water

* Recreate ebalance weights for balance plots
//drop balance_zero
ebalance time_zero $cvars_demographics, generate(balance_zero) targets(3)
svyset [pweight = balance_zero]

global treatments time_zero 

estimates clear

foreach y of varlist $treatments {
    qui reg `y' $cvars_demographics
    estimates store T0_`y'

    qui svy: reg `y' $cvars_demographics
    estimates store T1_`y'
}

coefplot T0_time_zero || T1_time_zero, ///
    drop(_cons) xline(0, lpattern(solid)) ///
    byopts(row(1)) levels(99 95) ///
    bylabels("(A) Treatment all" "(B) Using weight") ///
    mlabel(cond(@pval<.001, "***", cond(@pval<.01, "**", cond(@pval<.05, "*", "")))) ///
    title("Balance test")

graph save   "${graph}/Afrobalance.gph", replace
graph export "${graph}/Afrobalance.eps", replace
graph export "${graph}/Afrobalance.pdf", as(pdf) replace

***************************************************************
* Appendix D – Full-sample robustness (31 countries)
***************************************************************

********************************************************************
* 0. LOAD DATA AND PREPARE VARIABLES
********************************************************************
use "${data}/R9_final.dta", clear

* Ensure surveyyear exists
capture confirm variable surveyyear
if _rc {
    capture confirm variable year
    if !_rc gen surveyyear = year
}

* Recreate z-outcomes for full sample (min–max 0–1)
global outcomes_groups demo_support auth_support demo_rated
foreach v of varlist $outcomes_groups {
    qui summ `v'
    gen z`v' = (`v' - r(min)) / (r(max) - r(min))
}
global zoutcomes_group1 zdemo_support zauth_support zdemo_rated  
su $zoutcomes_group1

* Treatment indicator
global treatments time_zero 

* Clear any previous survey settings
svyset, clear

* Entropy balancing weights
// drop balance_zero   // uncomment if balance_zero already exists
ebalance time_zero $cvars, generate(balance_zero) targets(3)
svyset [pweight = balance_zero]

* Clean old files (optional – kept from your template)
capture erase "${table}/t1_e.xls"
capture erase "${table}/t1_e.rtf"
estimates clear

* Make sure esttab / eststo / estadd are available
cap which esttab
if _rc ssc install estout, replace

local models

foreach k of varlist $zoutcomes_group1 {
    
    *--------------------------
    * 1) Run model
    *--------------------------
    quietly svy: reg `k' i.time_zero $cvars i.cntrynum i.surveyyear
    
    *--------------------------
    * 2) Control-group summary
    *--------------------------
    quietly summarize `k' if time_zero == 0
    local m   = r(mean)
    local min = r(min)
    local max = r(max)

    *--------------------------
    * 3) Short titles for table
    *--------------------------
    local short_title ""
    if "`k'" == "zdemo_support" local short_title "Support for democracy"
    if "`k'" == "zauth_support" local short_title "Reject authoritarianism"
    if "`k'" == "zdemo_rated"   local short_title "Democracy rating"

    *--------------------------
    * 4) Add custom stats to e()
    *--------------------------
    qui estadd scalar cmean = `m'
    qui estadd scalar cmin  = `min'
    qui estadd scalar cmax  = `max'
    qui estadd local  pretreat "Yes"

    * Store model with nice title
    eststo, title("`short_title'")
    local models `"`models' `e(name)'"'
}

*------------------------------------------
* PRINT TABLE TO RESULTS WINDOW
*------------------------------------------
esttab `models', ///
    keep(1.time_zero) ///
    b(3) se level(95) brackets ///
    star(* 0.05 ** 0.01 *** 0.001) ///
    label nonotes noobs nobaselevels ///
    coeflabels(1.time_zero "Terrorism exposure") ///
    mtitle ///
    stats(N r2 pretreat cmean, ///
          fmt(0 3 0 3 ) ///
          labels("Observations" "R^2" "Pre-treatment" "Control Mean" "Min" "Max")) ///
    compress

*-------------------------------*
* EXPORT CSV
*-------------------------------*
esttab `models' using "${table}/appendixD_fullsample.csv", replace csv ///
    keep(1.time_zero) ///
    coeflabels(1.time_zero "Terrorism exposure") ///
    b(3) se level(95) brackets ///
    star(* 0.05 ** 0.01 *** 0.001) ///
    mtitle ///
    label nonotes noobs nobaselevels ///
    stats(N r2 pretreat cmean cmin cmax, ///
          fmt(0 3 0 3 3 3) ///
          labels("Observations" "R^2" "Pre-treatment" "Control mean" "Min" "Max")) ///
    compress
***************************************************************
* 12. PUT ALL GRAPHS INTO ONE DOCX
***************************************************************

putdocx begin

putdocx paragraph, style(Heading1)
putdocx text ("Results")

* Figure 1: vdemhdi
graph use "${graph}/vdemhdi.gph"
graph export "${graph}/vdemhdi.png", width(3000) replace
putdocx paragraph, halign(center)
putdocx image ("${graph}/vdemhdi.png"), width(14cm)
putdocx paragraph
putdocx text ("Figure 1. V-Dem vs HDI"), italic

* Figure 2: fatalities
graph use "${graph}/fatalities.gph"
graph export "${graph}/fatalities.png", width(3000) replace
putdocx paragraph, halign(center)
putdocx image ("${graph}/fatalities.png"), width(14cm)
putdocx paragraph
putdocx text ("Figure 2. Fatalities over time"), italic

* Figure 3: libdem
graph use "${graph}/libdem.gph"
graph export "${graph}/libdem.png", width(3000) replace
putdocx paragraph, halign(center)
putdocx image ("${graph}/libdem.png"), width(14cm)
putdocx paragraph
putdocx text ("Figure 3. Liberal democracy index (V-Dem)"), italic

* Figure 4: hdi
graph use "${graph}/hdi.gph"
graph export "${graph}/hdi.png", width(3000) replace
putdocx paragraph, halign(center)
putdocx image ("${graph}/hdi.png"), width(14cm)
putdocx paragraph
putdocx text ("Figure 4. Human Development Index (HDI)"), italic

* Figure 5: gender
graph use "${graph}/gender.gph"
graph export "${graph}/gender.png", width(3000) replace
putdocx paragraph, halign(center)
putdocx image ("${graph}/gender.png"), width(14cm)
putdocx paragraph
putdocx text ("Figure 5. Gender differences"), italic

* Figure 6: age
graph use "${graph}/age.gph"
graph export "${graph}/age.png", width(3000) replace
putdocx paragraph, halign(center)
putdocx image ("${graph}/age.png"), width(14cm)
putdocx paragraph
putdocx text ("Figure 6. Age effects"), italic

* Figure 7: Cross-country regression – Afrobarometer Round 9 and ACLED 2021-2023
graph use "${graph}/DV1_countries.gph"
graph export "${graph}/DV1_countries.png", width(3000) replace
putdocx paragraph, halign(center)
putdocx image ("${graph}/DV1_countries.png"), width(14cm)
putdocx paragraph
putdocx text ("Figure 7. Country-specific DV1 results"), italic

*----RUN this section after running the robustness results ----

* Figure 8: Cross-country regression – Afrobarometer Round 8 and GTD data,2019-2021
graph use "${graph}/DV1_cntryr8gtd.gph"
graph export "${graph}/DV1_cntryr8gtd.png", width(3000) replace
putdocx paragraph, halign(center)
putdocx image ("${graph}/DV1_cntryr8gtd.png"), width(14cm)
putdocx paragraph
putdocx text ("Figure 8. Country-specific DV1 results (R8-GTD)"), italic

* Figure 9: Afrobalance
graph use "${graph}/Afrobalance.gph"
graph export "${graph}/Afrobalance.png", width(3000) replace
putdocx paragraph, halign(center)
putdocx image ("${graph}/Afrobalance.png"), width(14cm)
putdocx paragraph
putdocx text ("Figure 9. Covariate balance (Afrobarometer)"), italic

* Figure 10: balance_afro (requires pre-existing graph file)
graph use "${graph}/balance_afro.gph"
graph export "${graph}/balance_afro.png", width(3000) replace
putdocx paragraph, halign(center)
putdocx image ("${graph}/balance_afro.png"), width(14cm)
putdocx paragraph
putdocx text ("Figure 10. Covariate balance (Afrobarometer, alternative)"), italic

putdocx save all_graphs.docx, replace

***************************************************************
* END OF DO-FILE
***************************************************************
